<?php

namespace App\Models\Entity\Asset;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
// 排序, composer require spatie/eloquent-sortable
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

/**
 * Class Vasseti
 * @package App\Models\Entity\Asset\Video
 * @version August 1, 2017, 4:32 pm UTC
 */
class Iaasset extends Model implements Sortable
{
    use SoftDeletes;
    
    use SortableTrait;

    public $sortable = [
        'order_column_name' => 'listorder',
        'sort_when_creating' => true,
    ];

    public $table = 'iaassets';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'iaasset_no',
        'name',
        'title',
        'subtitle',
        'score',
        'season',
        'episode',
        'vcat_id',
        'short_desc',
        'long_desc',
        // 'sp_id',
        // 'cp_id',
        // 'director_id',
        // 'actor_id',
        'created_at',
        'updated_at',
        'firstdate',
        'charge_type',
        'listorder',

        // 上下线, 发布
        'online_status',
        'moderate_status',
        'publish_status',
        'moderate_by',
        'moderate_at',
        'online_at',
        'offline_at',
        'user_id',
        'publish_down_status',

        // 注入
        'msg_id',
        'sync_result',
        'sync_content',
        'sync_description',
        'async_result',
        'async_content',
        'async_description',

        'iaasset_type',
        'upto',
        'serials',
        'subvcat_id'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'iaasset_no'        => 'string',
        'name'              => 'string',
        'title'             => 'string',
        'subtitle'          => 'string',
        'season'            => 'string',
        'episode'           => 'integer',
        'vcat_id'           => 'integer',
        'short_desc'        => 'string',
        'long_desc'         => 'string',
        'charge_type'       => 'tinyInteger',
        'listorder'         => 'integer',
        'serials'=>'integer',
        // 注入
        'msg_id'            => 'string',
        'sync_result'       => 'tinyInteger',
        'sync_content'      => 'string',
        'sync_description'  => 'string',
        'async_result'      => 'tinyInteger',
        'async_content'     => 'string',
        'async_description' => 'string',
        'iaasset_type'      => 'tinyInteger',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'iaasset_no'        => 'required|max:100|unique:vassetis,iaasset_no',
        'name'              => 'required|max:100',
        'title'             => 'max:100',
        'subtitle'          => 'max:100',
        'score'             => 'numeric',
        'season'            => 'numeric',
        'episode'           => 'numeric',
        'vcat_id'           => 'required|max:10',
        'short_desc'        => 'max:100',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 归类
     **/
    public function vcat()
    {
        return $this->hasOne(\App\Models\Common\CatData\Video\Vcat::class, 'id', 'vcat_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 年代
     **/
    public function vyears()
    {
        return $this->belongsToMany(\App\Models\Common\Enumdata\Enumop\Video\Vyear::class, 'iaasset_vyears', 'iaasset_id', 'vyear_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 地区
     **/
    public function vareas()
    {
        return $this->belongsToMany(\App\Models\Common\Enumdata\Enumop\Video\Varea::class, 'iaasset_vareas', 'iaasset_id', 'varea_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 语种
     **/
    public function vlangs()
    {
        return $this->belongsToMany(\App\Models\Common\Enumdata\Enumop\Video\Vlang::class, 'iaasset_vlangs', 'iaasset_id', 'vlang_id');
    }

    /**
     * 服务商
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function sps()
    {
        return $this->belongsToMany(\App\Models\Common\EntityData\Channel\Sp::class, 'iaasset_sps', 'iaasset_id', 'sp_id');
    }

    /**
     * 内容商
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function cps()
    {
        return $this->belongsToMany(\App\Models\Common\EntityData\Channel\Cp::class, 'iaasset_cps', 'iaasset_id', 'cp_id');
    }

    /**
     * 导演
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function directors()
    {
        return $this->belongsToMany(\App\Models\Common\EntityData\Video\Director::class, 'iaasset_directors', 'iaasset_id', 'director_id');
    }

    /**
     * 演员
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function actors()
    {
        return $this->belongsToMany(\App\Models\Common\EntityData\Video\Actor::class, 'iaasset_actors', 'iaasset_id', 'actor_id');
    }

    /**
     * 海报
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function vposteris()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Video\Media\Vposteri::class, 'iaasset_vposteris', 'iaasset_id', 'vposteri_id');
    }

    /**
     * 分集
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     **/
    public function iaserialis()
    {
        return $this->hasMany(\App\Models\Entity\Asset\Iaseriali::class, 'iaasset_id', 'id');
    }

    /**
     * 文件
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function vifiles()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Video\Media\Vifile::class, 'iaasset_vifiles', 'iaasset_id', 'vifile_id');
    }

    /**
     * 分类
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function vcattags()
    {
        return $this->belongsToMany(\App\Models\Common\TagData\Video\Vcattag::class, 'iaasset_vcattags', 'iaasset_id', 'vcattag_id');
    }

    /**
     * 专辑
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function albums()
    {
        return $this->belongsToMany(\App\Models\Entity\Column\Album::class, 'album_vassetis', 'iaasset_id', 'album_id');
    }

    /**
    * 过滤上下线数据
    **/
    public function scopeOnlineStatus($query, $status = 1)
    {
        return $query->where('online_status', $status);
    }

    /**
    * 过滤上线发布数据
    **/
    public function scopeOnlinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 1, 'publish_status' => $status]);
    }

    /**
    * 过滤下线发布数据
    **/
    public function scopeOfflinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 0, 'publish_down_status' => $status]);
    }

    /**
     * 获得此媒资的所有标签。
     */
    public function tags()
    {
        return $this->morphToMany(\App\Models\Entity\Tag\tag::class, 'tag_content');
    }

    /**
     * 获得审核信息。
     */
    public function audits()
    {
        return $this->morphMany('App\Models\Entity\content_audit\content_audit', 'auditable');
    }

    /**
     * 限制查询只包含用户所属SP媒资信息。
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeSpid($query)
    {
        if(!Admin::user()->isAdministrator()){
            return $query->whereHas('sps', function ($query) {
                $query->where('sp_id', '=', Admin::user()->sp_id);
            });
        }
    }

}
